package com.example.jimanguang.web.admin;

import antlr.ASTFactory;

import com.example.jimanguang.Services.UserService;
import com.example.jimanguang.constants.ApiConstant;
import com.example.jimanguang.web.models.ApiResult;
import com.example.jimanguang.web.models.VmUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;


@RestController
@RequestMapping(ApiConstant.ROUTE_USER_ROOT)
public class UserApiController {

    private final UserService service;
    @Autowired
    public UserApiController(UserService service) {
        this.service = service;
    }
    //封装成获取所有用户api
    //使用于用户管理
@GetMapping(ApiConstant.ROUTE_USER_ALL)
public ApiResult getUsers(@RequestParam String token){
    ArrayList<VmUser> users = new ArrayList<>();
    service.get().forEach(u->users.add(VmUser.create(u,true)));
   return new ApiResult(true,users.size()+"",users);
}
    //封装成获取所有用户api
    //使用于用户管理
    @GetMapping(ApiConstant.ROUTE_USER_PAGE)
public ApiResult getPagedUsers(@RequestParam int page, @RequestParam int size , @RequestParam String token)
{
    ArrayList<VmUser> users = new ArrayList<>();
    service.get(page, size).forEach(u->users.add(VmUser.create(u,true)));
    return new ApiResult(true,users.size()+"",users);
}

    //封装成获取所有用户api
    //使用于用户管理
    @GetMapping(ApiConstant.ROUTE_USER_SEARCh)
    public ApiResult search(@RequestParam String token, @PathVariable String kw)
    {
        ArrayList<VmUser> users = new ArrayList<>();
        service.search(kw).forEach(u->users.add(VmUser.create(u,true)));
        return new ApiResult(true,users.size()+"",users);
    }



}
